﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DAL;

namespace HomeStayWebCP.Controllers.Admin
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/

        public ActionResult Index()
        {
            if (HttpContext.Session["roleID"] != null && !HttpContext.Session["roleID"].ToString().Equals(String.Empty))
            {
                return View();
            }
            else
            {
                //redirect to You doesn't have permission
                return RedirectToAction("Login", "Admin");
            } 
        }

        private List<Report_Users_TopPostUsers_View> GetTopPostUser(string yearFrom, string yearTo)
        {
            //int topNo = 0;
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            List<Report_Users_TopPostUsers_View> listTopPostUser = ent.Report_Users_GetTopPostUser(yearFrom, yearTo).ToList<Report_Users_TopPostUsers_View>();
            return listTopPostUser;
        }

        private List<Report_Posts_TopRegions_View> GetTopRegion(string yearFrom, string yearTo)
        {
            //int topNo = 0;
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            List<Report_Posts_TopRegions_View> listTopRegion = ent.Report_Posts_GetTopRegion(yearFrom, yearTo).ToList<Report_Posts_TopRegions_View>();
            return listTopRegion;
        }

        private List<Report_UserInMonths_View> GetTopUserInMonths(string yearFrom)
        {
            //int topNo = 0;
            HomestayWebDBEntities ent = new HomestayWebDBEntities();
            List<Report_UserInMonths_View> listTopUserInMonths = ent.Report_Users_GetUserInMonths(yearFrom).ToList<Report_UserInMonths_View>();
            return listTopUserInMonths;
        }

        [HttpPost]
        public JsonResult ProcessTopRegionNewWin(string txtYearFrom, string txtYearTo)
        {
            List<Report_Posts_TopRegions_View> listTopRegion = GetTopRegion(txtYearFrom, txtYearTo);
            if (listTopRegion.Count > 0)
            {
                this.HttpContext.Session["Rpt_TopRegionSource"] = listTopRegion;
                this.HttpContext.Session["Rpt_TopRegionName"] = "TopRegion.rpt";
                this.HttpContext.Session["rptTopRegionFromDate"] = txtYearFrom;
                this.HttpContext.Session["rptTopRegionToDate"] = txtYearTo;

                return Json(true, "application/json");
            }
            else
            {
                return Json(false, "application/json");
            }
        }

        [HttpPost]
        public JsonResult ProcessTopPostUserNewWin(string txtYearFrom, string txtYearTo)
        {
            List<Report_Users_TopPostUsers_View> listTopPostUser = GetTopPostUser(txtYearFrom, txtYearTo);
            if (listTopPostUser.Count > 0)
            {
                this.HttpContext.Session["Rpt_TopPostUserSource"] = listTopPostUser;
                this.HttpContext.Session["Rpt_TopPostUserName"] = "TopPostUsers.rpt";
                this.HttpContext.Session["rptTopPostUserFromDate"] = txtYearFrom;
                this.HttpContext.Session["rptTopPostUserToDate"] = txtYearTo;
                return Json(true, "application/json");
            }
            else
            {
                return Json(false, "application/json");
            }
        }

        [HttpPost]
        public JsonResult ProcessTopUserInMonthsNewWin(string txtYear)
        {
            List<Report_UserInMonths_View> listUserinMonth = GetTopUserInMonths(txtYear);
            if (listUserinMonth.Count > 0)
            {
                this.HttpContext.Session["Rpt_TopUserInMonthsSource"] = listUserinMonth;
                this.HttpContext.Session["Rpt_TopUserInMonthsName"] = "TopUserInMonths.rpt";
                this.HttpContext.Session["rptUserInMonthsFromYear"] = txtYear;
                return Json(true, "application/json");
            }
            else
            {
                return Json(false, "application/json");
            }
        }

    }
}
